<?php

namespace app\common\model;

use think\Model;

class Institution extends Model
{
    public function add_one($info){
        $insert['display'] = 1;
        $insert['inputtime'] = time();
        $insert['name'] = $info['name'];
        $insert['note'] = $info['note'];
        $insert['count'] = 0;
        $insert['founder'] = $info['founder'];

        return $this->insertGetId($insert);
    }

     function getList($page,$pageSize,$search,$order = [],$field=['id','inputtime','name','note','count','founder']){
        $currentPage = $page;
        $page = ($page - 1) * $pageSize;
        $where = ['name'];
        $info = self::field($field)->withSearch($where,$search )->where(['display'=>1])->order($order)->limit($page,$pageSize)->select();
        $count = self::withSearch($where,$search )->where(['display'=>1])->count();
        //todo 创建人名称
        foreach ($info as $infoKey => $infoValue){
            $info[$infoKey]['inputtime'] = date('Y-m-d H:i',$infoValue['inputtime']);
        }
        $arr =  ['list'=>$info,'count'=>$count,'page'=>$currentPage,'pageSize'=>$pageSize];
        return $arr;
    }

    public function get_one($id,$field='*'){
        return $this->field($field)->where(['id'=>$id,'display'=>1])->find();
    }

    public function edit($id,$info){
        $update['updatetime'] = time();
        $update['name'] = $info['name'];
        $update['note'] = $info['note'];

        return $this->where(['id'=>$id,'display'=>1])->update($update);
    }

    public function del($id){
        return $this->where(['id'=>$id,'display'=>1])->update(['display'=>0]);
    }

    public function searchNameAttr($query,$value, $data)
    {
        if($value){
            $query->where('name','like', '%'.$value . '%');
        }

    }

    public function Inc_count($id){
        return $this->where(['id'=>$id,'display'=>1])->setInc('count',1);
    }

    public function Dec_count($id){
        return $this->where(['id'=>$id,'display'=>1])->setDec('count',1);
    }

    public function get_arr(){
        return $this->field(['id','name'])->where(['display'=>1])->select();
    }
}
